package org.apache.solr.handler.dataimport.scheduler;

import org.apache.solr.core.SolrResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * Uses java.util.Properties to load scheduler configuration from dataimport.properties
 * @author mbonaci
 */
public class SolrDataImportProperties {
    private Properties properties;

    public static final String SYNC_ENABLED = "syncEnabled";
    public static final String SYNC_CORES = "syncCores";
    public static final String SERVER = "server";
    public static final String PORT = "port";
    public static final String WEBAPP = "webapp";
    public static final String PARAMS = "params";
    public static final String INTERVAL = "interval";

    private static final Logger logger = LoggerFactory.getLogger(SolrDataImportProperties.class);

    public SolrDataImportProperties() {
//          loadProperties(true);
    }

    public void loadProperties(boolean force) {
        try {
            SolrResourceLoader loader = new SolrResourceLoader();
            logger.info("Data dir = " + loader.getDataDir());
            logger.info("Config dir = " + loader.getConfigDir());

            String configDir = loader.getConfigDir();
            configDir = SolrResourceLoader.normalizeDir(configDir);
            if (force || properties == null) {
                properties = new Properties();

                File dataImportProperties = new File(configDir, "dataimport.properties");

                FileInputStream fis = new FileInputStream(dataImportProperties);
                properties.load(fis);
            }
        } catch (FileNotFoundException fnfe) {
            logger.error("Error locating DataImportScheduler dataimport.properties file", fnfe);
        } catch (IOException ioe) {
            logger.error("Error reading DataImportScheduler dataimport.properties file", ioe);
        } catch (Exception e) {
            logger.error("Error loading DataImportScheduler properties", e);
        }
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }
}
